#!/usr/bin/env python

# Carri: Jordi Carrillo's python modules
# Copyright (C) 2009 Jordi Carrillo Bosch
#
# This file is part of Carri Project.
#
# Carri is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Carri is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Carri.  If not, see <http://www.gnu.org/licenses/>.



# Walks a directory structure and renames its files under it taking into 
# account the modification times.


import os,sys,re
from stat import *
from operator import itemgetter
from camina import Walker,Renamer

count = 0
walker = Walker(sys.argv[1],regexfile = ('jpg',re.I))
renamer = Renamer('anyname','.jpg',23000)
pics = {}
for file in walker.search():
    print "processing "+file
    mtime = os.stat(file)[ST_MTIME]
    pics[file] = mtime

orderedPics = sorted(pics.iteritems(), key=itemgetter(1))
for picdate in orderedPics:
    print "renaming "+picdate[0]
    renamer.rename(picdate[0])


